require "rubygems"
require "win32ole"
require "watir-classic"

$LOAD_PATH << './step_definitions'
$LOAD_PATH << './features/step_definitions'

$ds_au3=WIN32OLE.new("AutoITX3.Control")
$generic_timeout = 10

def retry_timeout(timeout, message, &func)
  t = timeout
  while (t > 0)
    t = t - 1
    result = func.call
    return result unless !result
    sleep 1
  end
  fail message
end

def find_text_field(browser, pattern, number = 1)
  num = number
  browser.text_fields.each { |f|
    num = num - 1 unless !f.id.match(pattern)
    return f unless !f.id.match(pattern) || num > 0
  }
  return nil
end

def find_text_field_by_name(browser, name)
  timeout = $generic_timeout
  sleep 1 until browser.text_field(:name, name).exists? || ((timeout = timeout - 1) <= 0)
  fail "Timeout waiting for '" + name + "' field ro appear" unless timeout > 0
  return @browser.text_field(:name, name)
end

def find_button_by_label(browser, label, number = 1)
  btns = browser.elements(:title, label)
  num = number
  btns.each { |btn|
    num = num - 1 if ((btn.class.method_defined?('value') and btn.value == label) or btn.text == label or btn.title == label) and btn.visible? and btn.enabled?
    return btn if num == 0
  }
  return nil
end

def close_all_browsers()
  limit = 100
  $ds_au3.opt "WinTitleMatchMode", 2
  # HTTP Auth popups for IE9
  while ($ds_au3.WinClose("Windows Security") != 0)
    limit = limit - 1
    raise "Failed to close all IE windows" if limit == 0
  end
  # HTTP Auth popups for IE8
  while ($ds_au3.WinClose("Connect to") != 0)
    limit = limit - 1
    raise "Failed to close all IE windows" if limit == 0
  end
  while ($ds_au3.WinClose("Internet Explorer") != 0)
    limit = limit - 1
    raise "Failed to close all IE windows" if limit == 0
  end
end

Given /^I have opened a page at URL "(.*)" in Internet Explorer$/ do |url|
  close_all_browsers
  sleep 1
  @browser=Watir::IE.new()
  @browser.maximize if $maximize_browser
  @browser.bring_to_front
  @browser.goto(url)
  hwnd = @browser.hwnd()
  @browser.wait
  @browser = Watir::IE.attach(:hwnd, hwnd)
end

When /^I type "([^\"]*)" in the field with id ending with "([^\"]*)"$/ do |text, idsuffix|
  field = retry_timeout($generic_timeout, "Timeout waiting field whding with '#{idsuffix}' to appear") { find_text_field(@browser, /.*#{idsuffix}/) }
  field.set(text)
end

When /^I click a button number ([0-9]*) with label "([^\"]*)"$/ do |number, label|
  button = retry_timeout($generic_timeout, "Timeout waiting for button number #{number} with label '#{idsuffix}' to appear") { find_button_by_label @browser, label, number }
  sleep 2
  button.click!
  @browser.wait
end

When /^I click "([^\"]*)" button$/ do |label|
  button = retry_timeout($generic_timeout, "Timeout waiting for button with label '#{label}' to appear") { find_button_by_label @browser, label }
  sleep 2
  button.click!
  @browser.wait
end

Given /^nothing$/ do
end

Then /^"(.*)" should be mentioned on the page$/ do |item|
  retry_timeout($generic_timeout, "Timeout waiting for '#{item}' to appear on the page") { @browser.text.include? item }
end

Then /^I close the browser$/ do
  @browser.close()
end

When /^I wait for (\d+) seconds?$/ do |time|
  sleep Integer(time)
end

